package tree.数的最大深度;
//给定一个二叉树，找出其最大深度。
//
// 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
//
// 说明: 叶子节点是指没有子节点的节点。
//
// 示例：
//给定二叉树 [3,9,20,null,null,15,7]，
//
//     3
//   / \
//  9  20
//    /  \
//   15   7
//
// 返回它的最大深度 3 。
// Related Topics 树 深度优先搜索 递归
// 👍 806 👎 0


//leetcode submit region begin(Prohibit modification and deletion)

/**
 * Definition for doubleDimensionalArrayPrint binary tree node.
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    public static void main(String[] args) {
        TreeNode l3 = new TreeNode(15);
        TreeNode r3 = new TreeNode(7);
        TreeNode r2 = new TreeNode(20, l3, r3);
        TreeNode l2 = new TreeNode(9);
        TreeNode treeNode = new TreeNode(3, l2, r2);
        System.out.println(maxDepth(treeNode));
    }

    public static int maxDepth(TreeNode root) {
        //树的深度 =  Max(dp(l),dp(r)) + 1
        return treeDepth(root);
    }

    public static int treeDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int l = 0;
        int r = 0;
        l = treeDepth(root.left) + 1;
        r = treeDepth(root.right) + 1;
        return l > r ? l : r;
    }
}
//leetcode submit region end(Prohibit modification and deletion)
